package bgai.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import bgai.gamelogic.Backgammon;
import bgai.gamelogic.CheckerColor;
import bgai.players.Door;
import bgai.players.DoorHit1;
import bgai.players.DoorHit2;
import bgai.players.Hit1;
import bgai.players.Hit2;
import bgai.players.Human;
import bgai.players.Player;
import bgai.players.Random;

/**
 * Finestra di selezione del tipo di giocatori (umano/computer)
 */
public class BgStart extends JFrame implements ActionListener {

	private static final long SEED= 2;
	
	/**
	 * Costante che definisce la stringa usata in {@link #blackPlayer} e {@link #whitePlayer}
	 * per definire il giocatore umano
	 */
	private static final String HUMAN= "Umano";

	private static final String DOOR_AI= "AI (door)";
	private static final String DOOR_HIT1_AI= "AI (door hit 1)";
	private static final String DOOR_HI21_AI= "AI (door hit 2)";
	private static final String HIT1_AI= "AI (hit 1)";
	private static final String HIT2_AI= "AI (hit 2)";
	private static final String RANDOM_AI= "AI (random)";
	
	private JComboBox<String> blackPlayer;
	private JComboBox<String> whitePlayer;
	private String[] playerTypes= {HUMAN, DOOR_AI, DOOR_HIT1_AI, DOOR_HI21_AI, HIT1_AI, HIT2_AI, RANDOM_AI};

	public static void main(String[] args){
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				BgStart frame= new BgStart();
				frame.setLocationRelativeTo(null); // al centro dello schermo
				frame.setVisible(true);
			}
		});
	}

	/**
	 * Crea una nuova finestra di inizio del gioco. La finestra permette
	 * la selezione dei giocatori (umano/computer).
	 */
	public BgStart() {
		createGUI();
	}

	private void createGUI(){

		setTitle("Io, Backgammon");

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(new Rectangle(600, 200));
		setResizable(false);
		//frame.addWindowListener(new myWindowListener());

		JPanel myPanel = new JPanel(new GridBagLayout());
		setContentPane(myPanel);

		JLabel label;
		GridBagConstraints layoutConst;
		
		// etichetta in alto
		label= new JLabel("Seleziona il tipo dei giocatori", JLabel.CENTER);
		layoutConst= new GridBagConstraints();
		layoutConst.gridx= 0;
		layoutConst.gridy= 0;
		layoutConst.gridwidth= 3;
		layoutConst.weightx= 1.0;
		layoutConst.fill= GridBagConstraints.HORIZONTAL;
		layoutConst.ipady= 5;
		myPanel.add(label, layoutConst);

		//tavola sinistra (usa BorderLayout)
		JPanel blackPanel = new JPanel(new GridBagLayout());
		blackPanel.setBackground(java.awt.Color.BLACK);
		// etichetta
		layoutConst= new GridBagConstraints();
		layoutConst.gridx= 0;
		layoutConst.gridy= 0;
		label= new JLabel("Giocatore Nero");
		label.setForeground(Color.WHITE);
		blackPanel.add(label, layoutConst);
		// combo box
		blackPlayer= new JComboBox<String>(playerTypes);
		layoutConst= new GridBagConstraints();
		layoutConst.gridx= 0;
		layoutConst.gridy= 1;
		blackPanel.add(blackPlayer, layoutConst);
		// fine
		layoutConst= new GridBagConstraints();
		layoutConst.gridx= 0;
		layoutConst.gridy= 1;
		layoutConst.weightx= 1.0;
		layoutConst.weighty= 1.0;
		layoutConst.fill= GridBagConstraints.BOTH;
		myPanel.add(blackPanel, layoutConst);

		//tavola destra (usa BorderLayout)
		JPanel whitePanel = new JPanel(new GridBagLayout());
		whitePanel.setBackground(java.awt.Color.WHITE);
		// etichetta
		layoutConst= new GridBagConstraints();
		layoutConst.gridx= 0;
		layoutConst.gridy= 0;
		label= new JLabel("Giocatore Bianco");
		label.setForeground(Color.BLACK);
		whitePanel.add(label, layoutConst);
		// combo box
		whitePlayer= new JComboBox<String>(playerTypes);
		layoutConst= new GridBagConstraints();
		layoutConst.gridx= 0;
		layoutConst.gridy= 1;
		whitePanel.add(whitePlayer, layoutConst);
		// finelayoutConst= new GridBagConstraints();
		layoutConst.gridx= 1;
		layoutConst.gridy= 1;
		layoutConst.fill= GridBagConstraints.BOTH;
		layoutConst.weightx= 1.0;
		layoutConst.weighty= 1.0;
		myPanel.add(whitePanel, layoutConst);

		// bottone inizia partita
		JButton start= new JButton("Inizia");
		start.addActionListener(this);
		layoutConst= new GridBagConstraints();
		layoutConst.gridx= 0;
		layoutConst.gridy= 2;
		layoutConst.gridwidth= 3;
		myPanel.add(start, layoutConst);
		
		//pack();
	}

	/**
	 * Azione per il pulsante "inizia partita"
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		Player black = null;
		Player white = null;
		int depth= 0;
		
		Object selItem;
		
		selItem= blackPlayer.getSelectedItem();
		
		if (!HUMAN.equals(selItem)) {
			// IA
			depth= 0;
			while (depth<1) {
				String str= JOptionPane.showInputDialog(
						"Inserisci profondita' di ricerca\n"+
						"per il giocatore NERO (>=1):", 1);
				if (str==null) return; // premuto annulla
				depth= Integer.parseInt(str);
			}
		}
		
		if (HUMAN.equals(selItem)) {
			black= new Human(CheckerColor.BLACK);
		} else if (DOOR_AI.equals(selItem)) {
			black= new Door(CheckerColor.BLACK, depth);
		} else if (DOOR_HIT1_AI.equals(selItem)) {
			black= new DoorHit1(CheckerColor.BLACK, depth);
		} else if (DOOR_HI21_AI.equals(selItem)) {
			black= new DoorHit2(CheckerColor.BLACK, depth);
		} else if (HIT1_AI.equals(selItem)) {
			black= new Hit1(CheckerColor.BLACK, depth);
		} else if (HIT2_AI.equals(selItem)) {
			black= new Hit2(CheckerColor.BLACK, depth);
		} else if (RANDOM_AI.equals(selItem)) {
			black= new Random(CheckerColor.BLACK, depth);
		}
		
		if (HUMAN.equals(selItem)) {
			System.out.println(CheckerColor.BLACK + " e' umano");
		} else {
			System.out.println(CheckerColor.BLACK + " e' computer ("
					+selItem+", depth="+depth+")");
		}
		
		selItem= whitePlayer.getSelectedItem();
		
		if (!HUMAN.equals(selItem)) {
			// IA
			depth= 0;
			while (depth<1) {
				String str= JOptionPane.showInputDialog(
						"Inserisci profondita' di ricerca\n"+
						"per il giocatore BIANCO (>=1):", 1);
				if (str==null) return; // premuto annulla
				depth= Integer.parseInt(str);
			}
		}
		
		if (HUMAN.equals(selItem)) {
			white= new Human(CheckerColor.WHITE);
		} else if (DOOR_AI.equals(selItem)) {
			white= new Door(CheckerColor.WHITE, depth);
		} else if (DOOR_HIT1_AI.equals(selItem)) {
			white= new DoorHit1(CheckerColor.WHITE, depth);
		} else if (DOOR_HI21_AI.equals(selItem)) {
			white= new DoorHit2(CheckerColor.WHITE, depth);
		} else if (HIT1_AI.equals(selItem)) {
			white= new Hit1(CheckerColor.WHITE, depth);
		} else if (HIT2_AI.equals(selItem)) {
			white= new Hit2(CheckerColor.WHITE, depth);
		} else if (RANDOM_AI.equals(selItem)) {
			white= new Random(CheckerColor.WHITE, depth);
		}
		
		if (HUMAN.equals(selItem)) {
			System.out.println(CheckerColor.WHITE + " e' umano");
		} else {
			System.out.println(CheckerColor.WHITE + " e' computer ("
					+selItem+", depth="+depth+")");
		}
		
		Backgammon bg= new Backgammon(black, white, SEED);
		BackgammonGUI bgGui= new BackgammonGUI(bg);
		
		// visualizzo e si parte!
		bgGui.setInitWindow(this);
		this.setVisible(false);
		bgGui.setVisible(true);
		
		bgGui.initGame();
		
	}

}
